﻿/*
 *  Author David 2012-9-13 10:20
 *  业务逻辑检查正常
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class OperatorRealtimeTransactionController : BenefitHelper.Tools.BaseControl
    {
        public ActionResult Index(int uid, string pdate)
        {
            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetSettlementModel(pdate, db);//获取查询的交易日
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            cookie.SetCookie("querydate", pdate, 20);//保存查询交易员，为在月，年数据里返回留数据
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (th != null && th.YPdate != 0)//如果该交易日不为空，查询当日交易数据
                {
                    int tid = th.Id;
                    int operatorId = uid;
                    BenefitHelper.Sys.Operator op = new BenefitHelper.Sys.Operator().GetModel(operatorId, db);//获取查询的交易员
                    ViewBag.op = op;//保存该交易员供页面显示调用

                    ViewBag.pdate = pdate;//保存该交易日供页面显示调用
                    string stringPdate = new BenefitHelper.Sys.TradeHistory().AddHorizontalLineIntoPdate(pdate);//为日期加上横线
                    ViewBag.Hpdate = stringPdate;//保存该横线日期供页面显示调用
                    bool traded = op.OperatorTradedAtDay(operatorId, tid, db);//判断该交易员当日是否有交易
                    if (traded)//如果有交易记录，查询当日交易数据
                    {
                        BenefitHelper.View.OperatorDayChange change = new BenefitHelper.View.OperatorDayChange();
                        change = change.GetOperatorDayChange(operatorId, tid, db);//查询当日交易数据
                        ViewBag.change = change;//保存当日交易数据供页面调用

                        BenefitHelper.View.OperatorAvgWeek changeweek = new BenefitHelper.View.OperatorAvgWeek().GetOperatorAvgWeek(operatorId, th.YPdate, th.WPdate, db);//查询交易员周均值
                        ViewBag.changeweek = changeweek;//保存交易员周均值数据供页面调用

                        BenefitHelper.View.OperatorFiveDayAvg changefiveday = new BenefitHelper.View.OperatorFiveDayAvg().GetOperatorFiveDayAvg(operatorId, tid, db);//五日均值
                        ViewBag.changefiveday = changefiveday;//保存五日均值数据供页面调用

                        BenefitHelper.View.OperatorMonthAvg changemonth = new BenefitHelper.View.OperatorMonthAvg().GetOperatorMonthAvg(operatorId, th.MPdate, th.YPdate, db);//当月均值
                        ViewBag.changemonth = changemonth;//保存当月均值数据供页面调用

                        BenefitHelper.Sys.DayGroupOperators dgos = new BenefitHelper.Sys.DayGroupOperators();
                        int groupid = dgos.GetGroupId(operatorId, Convert.ToDateTime(stringPdate), db);//查询当日交易员所在小组
                        BenefitHelper.View.GroupDayAvg changegroup = new BenefitHelper.View.GroupDayAvg().GetGroupAvg(groupid, tid, db);//查询小组均值
                        ViewBag.changegroup = changegroup;//保存小组均值数据供页面调用

                        BenefitHelper.Data.Day.CompanyAvgDay companyavgday = new BenefitHelper.Data.Day.CompanyAvgDay().GetCompanyAvgDay(tid, db);//查询公司均值
                        ViewBag.companyavgday = companyavgday;//保存公司均值数据供页面调用

                        BenefitHelper.Data.Day.BatchAvgDay batchavgday = new BenefitHelper.Data.Day.BatchAvgDay().GetBatchAvgDay(operatorId, tid, db);//查询同批次交易员均值
                        ViewBag.batchavgday = batchavgday;//保存同批次交易员均值数据供页面调用

                        BenefitHelper.View.OperatorWeekSum operatorweeksum = new BenefitHelper.View.OperatorWeekSum().GetOperatorWeekSum(operatorId, th.YPdate, th.MPdate, th.WPdate, db);//查询交易员周累计
                        ViewBag.operatorweeksum = operatorweeksum;//保存交易员周累计数据供页面调用

                        BenefitHelper.View.OperatorMonthSum operatormonthsum = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(operatorId, th.MPdate, th.YPdate, db);//查询交易员月累计
                        ViewBag.operatormonthsum = operatormonthsum;//保存交易员月累计数据供页面调用

                        BenefitHelper.View.OperatorYearSum operatoryearsum = new BenefitHelper.View.OperatorYearSum().GetOperatorYearSum(operatorId, th.YPdate, db);//查询交易员年累计
                        ViewBag.operatoryearsum = operatoryearsum;//保存交易员年累计数据供页面调用

                        BenefitHelper.View.OperatorWeekExpected operatorweekexpected = new BenefitHelper.View.OperatorWeekExpected().GetOperatorWeekExpected(operatorId, th.WPdate, th.YPdate, db);//查询交易员周预期
                        ViewBag.operatorweekexpected = operatorweekexpected;//保存交易员周预期数据供页面调用

                        int status = BenefitHelper.Sys.OperatorStatus.GetOperatorStatus(change.Profit);
                        ViewBag.status = status;//保存交易员状态码
                        ViewBag.statusstring = BenefitHelper.Sys.OperatorStatus.GetOperatorStatus(status);//保存交易员状态
                        string js = BenefitHelper.Tools.ChartAtManager.GetChartJSStringOnHZXI(tid, operatorId, db);//得到显示的图表

                        BenefitHelper.Data.Day.OperatorPublish publish = new BenefitHelper.Data.Day.OperatorPublish();
                        //publish = publish.GetOperatorPublish(tid, operatorId, db);//获取交易员当日罚款
                        //ViewBag.publish = publish;



                        ViewBag.js = js;
                        db.Dispose();
                        return View();
                    }//没有交易记录，跳转到错误页面
                    else
                    {
                        db.Dispose();
                        return Redirect("/Error/index/" + uid + "/" + pdate);
                    }
                }//交易日为空，跳转到错误页面
                else
                {
                    db.Dispose();
                    return Redirect("/Error/index/" + uid + "/" + pdate);
                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
        }

    }
}
